<html>
<head>
    <meta charset="utf-8">
    <title>绑定手机号</title>
    <#assign base=request.contextPath  />
    <#setting  number_format="0.##"/>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${base}/frame/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${base}/sweep/css/app.css" media="all">
    <link rel="stylesheet" href="${base}/sweep/css/mui.min.css" media="all">
    <link rel="stylesheet" type="text/css" href="${base}/sweep/css/style.css" />
    <script src="${base}/backstage/js/jquery-1.8.3.min.js"></script>
    <script src="${base}/sweep/js/mui.min.js"></script>
    <script src="${base}/backstage/layui/layui.js"></script>
</head>
<body>
<div class="mui-content">
    <div class="mui-content-padded" style="margin: 5px;">
        <div class="mui-input-row">
            <input type="text" id="phone" class="mui-input-clear" placeholder="请输入联系电话">
        </div>
<#--        <button type="button" id="valCode" class="mui-btn mui-btn-warning" onclick="code()" style="float: right">获取验证码</button>-->
        <input type="button" class="obtain" id="valCode" onclick="code()" style="float: right;height: 40px;" value="获取验证码">
        <div class="mui-input-row">
            <input type="text" id="code" class="ui-alert" placeholder="请输入验证码">
        </div>
        <div align="center">
            <button type="button" class="layui-btn" id="saveButton" style="width: 100%">保存</button>
        </div>
    </div>
</div>
</body>
<script>

    layui.use('layer', function() { //独立版的layer无需执行这一句
        var $ = layui.jquery,
            layer = layui.layer; //独立版的layer无需执行这一句
    })

    $("#saveButton").on("click", function () {
        if ($("#phone").val()=="") {
            layer.msg("请填写联系电话！")
            return;
        }

            $.ajax({
                type: "GET",
                url: "${base}/wx/user/equalsCache",
                data: {phone:$("#phone").val(),code:$("#code").val()},
                success: function (data) {
                    if(data.code==200){
                        sendSms();
                    }else{
                        layer.msg("验证码不符")
                    }
                }
            });



    })


    function sendSms() {
        $.ajax({
            type: "GET",
            url: "${base}/wx/user/updatePhone",
            data: {phone:$("#phone").val(),userId:'${user.id}'},
            success: function (data) {
                if (data.code === 200) {
                    layer.msg(data.msg)
                    window.location.href="${base}/wx/user/personal.html";
                } else {
                    layer.msg(data.msg)
                }
            }
        });
    }




    //获取验证码
    function code() {

        if($("#phone").val()==""){
            layer.msg("请输入手机号");
            return
        }
        var phone = $("#phone").val();
        if(!(/^1[3456789]\d{9}$/.test(phone))){
            mui.toast("手机号格式有误，请重填");
            return false;
        }

        $.ajax({
            type: "GET",
            url: "${base}/wx/user/sendSms",
            data: {phone:$("#phone").val()},
            success: function (data) {
                if (data.code === 200) {
                    layer.msg(data.msg)
                    //获取成功
                    getTime()
                } else {
                    layer.msg(data.msg)
                }
            }
        });
    }
    var countdown = 60;
    function getTime() {
        if(countdown==0){
            $("#valCode").attr("disabled",false);
            $("#valCode").val("获取验证码");
            countdown = 60;
            return false;
        }else{
            $("#valCode").attr("disabled", true);
            $("#valCode").val("重新发送(" + countdown + ")");
            countdown--;
        }
        setTimeout(function() {
            getTime();
        },1000);
    }
</script>
</html>


